package class1;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.MysqlMethod;
import student.list.updateStudent.SelectStudentInfo;
import student.list.updateStudent.SelectStudentList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;
@WebServlet("/class1")
public class UpdateClass extends HttpServlet {
    @Override
    /**
     * 通过前端传过来的Id查询班级详情并返回给前端
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");//转码
        UpdateClassList updateClassList = new UpdateClassList();
        updateClassList.setCode(0);//设置code值,0代表请求成功
        updateClassList.setMsg("success");//设置MSg的值
        //获取前端传过来的studentId
        String  classId1 = req.getParameter("classId");
        Integer classId = Integer.valueOf(classId1);
        System.out.println(classId);
        String[] a = MysqlMethod.selectSingleclass(classId);//查询数据库,获取数据
        //数据类型转换
        Integer a1 = Integer.valueOf(a[0]);
        Integer a2 = Integer.valueOf(a[5]);
        //给selectStudentInfo对象赋初值
        ClassInfo classInfo = new ClassInfo(a1,a[1],a[2],a[3],a[4],a2);
        //把selectStudentInfo的值加入selectStudentList(selectStudentInfo是selectStudentList的一个属性,所以也要把selectStudentInfo的值赋给selectStudentList)
        updateClassList.setData(classInfo);
        //将selectStudentList转为json格式
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(updateClassList);
        //向前端传值
        resp.getWriter().write(result);
    }
    @Override
    /**
     * 修改班级方法
     */
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //在doPUt中要用流的方法获取前端传过来的参数
        BufferedReader reader = req.getReader();
        String classId = reader.readLine();
        reader.close();//关闭流
        String str1 = URLDecoder.decode(classId,"utf-8");//将获取的数据进行转码
        String[] str =  MysqlMethod.split03(str1);//将获取的字符串进行拆分得到数据
        //将获取到String类型的数据转为Interget类型
        Integer classIdstr = Integer.valueOf(str[0]);
        Integer typestr = Integer.valueOf(str[5]);
        //调用方法进行修改
        MysqlMethod.update02(classIdstr,str[1],str[2],str[3],str[4],typestr);
    }

    /**
     * 删除班级方法
     */
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传过来的studentId
        String  classId1 = req.getParameter("classId");
        int classId = Integer.parseInt(classId1);
        //调用方法删除学生信息
        MysqlMethod.delete02(classId);
    }

    /**
     *添加班级的类
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//转码
        //创建对象
        ClassListView classListView = new ClassListView();
        classListView.setCode(0);//code为0说明请求成功
        classListView.setMsg("success");
        classListView.setTotal(4);//设置要传递的参数的个数
        //获取前端发送过来的数据
        String classNo = req.getParameter("classNo");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String remark  = req.getParameter("remark");
        System.out.println(classNo);
        System.out.println(name);
        System.out.println(type);
        System.out.println(remark);
        //将获取的值传入数据库进行添加
        MysqlMethod.addClass(classNo,name,remark,type);
    }
}
